package com.siyue.bean.vo;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;

import java.util.ArrayList;
import java.util.List;

public class AccessDeviceExcelListener extends AnalysisEventListener {

    List<String> ipList=new ArrayList<>();

    @Override
    public void invoke(Object data, AnalysisContext context) {
        //校验名称
        AccessDeviceExcelVo excelVo= (AccessDeviceExcelVo) data;
        //校验名称
        if (StrUtil.isBlank(excelVo.getName())) {
            throw new RuntimeException(String.format("第%s行【名称】为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        //ip地址
        if (StrUtil.isBlank(excelVo.getIpaddr())) {
            throw new RuntimeException(String.format("第%s行【ip地址】为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }else{

            //校验ip格式是否正确
            String regex ="([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
            boolean bool=excelVo.getIpaddr().matches(regex);
            if (!bool){
                throw new RuntimeException(String.format("第%s行【ip地址】格式不正确，请核实", context.readRowHolder().getRowIndex() + 1));
            }
        }
        //判断ip是否重复
        if (ipList.contains(excelVo.getIpaddr())){
            throw new RuntimeException(String.format("第%s行【ip地址】重复，请核实", context.readRowHolder().getRowIndex() + 1));
        }else{
            ipList.add(excelVo.getIpaddr());
        }


        //端口
        if (StrUtil.isBlank(excelVo.getPort())) {
            throw new RuntimeException(String.format("第%s行【端口】为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }else{
            String regex ="^(\\d{1,5}|[1-5]\\d{5}|6[0-4]\\d{4}|65[0-4]\\d{3}|655[0-2]\\d{2}|6553[0-5])$";
            boolean bool=excelVo.getPort().matches(regex);
            if (!bool){
                throw new RuntimeException(String.format("第%s行【端口】格式不正确，请核实", context.readRowHolder().getRowIndex() + 1));
            }
        }
        //账号
        if (StrUtil.isBlank(excelVo.getUsername())) {
            throw new RuntimeException(String.format("第%s行【账号】为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        //密码
        if (StrUtil.isBlank(excelVo.getPassword())) {
            throw new RuntimeException(String.format("第%s行【密码】为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        if (exception instanceof ExcelDataConvertException) {
            /**从0开始计算*/
            Integer columnIndex = ((ExcelDataConvertException) exception).getColumnIndex() + 1;
            Integer rowIndex = ((ExcelDataConvertException) exception).getRowIndex() + 1;
            String message = "第" + rowIndex + "行，第" + columnIndex + "列" + "数据格式有误，请核实";
            throw new RuntimeException(message);
        } else if (exception instanceof RuntimeException) {
            throw exception;
        } else {
            super.onException(exception, context);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //解析完,全部回调逻辑实现
        ipList.clear();
    }
}
